package com.xbongbong.crm.perform.query.list.button;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.weblist.pojo.TagPojo;
import com.xbongbong.saas.domain.entity.TagLinkEntity;
import com.xbongbong.saas.enums.TagEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.TagLinkModel;
import org.elasticsearch.common.inject.Singleton;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/12/20 20:40
 */
@Component
public class PublicTag {

    @Resource
    private TagLinkModel tagLinkModel;

    private String attr;
    /**
     * 公海标签map
     */
    private Map<Long,String> publicTagMap;


    private Map<Long, TagLinkEntity> tagLinkMap;


    public Map<Long, String> getPublicTagMap() {
        return publicTagMap;
    }

    public Map<Long, TagLinkEntity> getTagLinkMap() {
        return tagLinkMap;
    }

    public void setPublicTagMap(Map<Long, String> publicTagMap) {
        this.publicTagMap = publicTagMap;
    }

    public PublicTag() {
    }

    public PublicTag(String attr, Map<Long, String> publicTagMap) {
        this.attr = attr;
        this.publicTagMap = publicTagMap;
    }

    public PublicTag( Map<Long, TagLinkEntity> tagLinkMap, String attr) {
        this.attr = attr;
        this.tagLinkMap = tagLinkMap;
    }


    public String getAttr() {
        return attr;
    }

    public void setAttr(String attr) {
        this.attr = attr + "Tag";
    }

    public PublicTag getPublicTag( String attr, String corpid, List<Long>dataIdList,Integer businessType) {
        Map<Long,String> publicTagMap = new HashMap<>();
        if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode())){
            publicTagMap = tagLinkModel.getPublicTag(corpid, dataIdList);
        }else if(Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())){
            publicTagMap = tagLinkModel.getCluePublicTag(corpid,dataIdList);
        }
        return new PublicTag(attr, publicTagMap);
    }
    /**
     * 获取转移标签
     * @param attr
     * @param corpid
     * @param dataIdList
     * @param refType 区分线索公海池和客户公海池
     * @return
     */
    public PublicTag getTransferTag(String attr, String corpid, List<Long>dataIdList,Integer refType) {
        Map<Long,String> publicTagMap = tagLinkModel.getTransferTag(corpid, dataIdList,refType);
        return new PublicTag(attr, publicTagMap);
    }

    public static PublicTag getTagLinkMap(TagLinkModel tagLinkModel, String attr, String corpid, List<Long>dataIdList, Integer businessType) {
        Map<Long, TagLinkEntity> publicTagMap = tagLinkModel.getPublicTagLink(corpid, dataIdList, businessType);
        return new PublicTag(publicTagMap, attr);
    }

    /**
     * “退”字标签
     * @param publicTagPojo
     * @param dataId
     * @param tagData
     * @return void
     * @author 吴峰
     * @date 2021/12/21 10:18
     */
    public static void setPublicTagData(PublicTag publicTagPojo, Long dataId, JSONObject tagData) {
        if (StringUtil.isNotEmpty(publicTagPojo.getAttr()) && publicTagPojo.getPublicTagMap().containsKey(dataId)) {
            JSONArray tagPojoList = tagData.getJSONArray(publicTagPojo.getAttr());
            tagPojoList = tagPojoList == null ? new JSONArray() : tagPojoList;
            TagPojo tagPojo = new TagPojo(TagEnum.TAG_RETURN_PUBLIC);
            tagPojo.setMemo(publicTagPojo.getPublicTagMap().get(dataId));
            tagPojoList.add(tagPojo);
            tagData.put(publicTagPojo.getAttr(), tagPojoList);
        }
    }

    /**
     * 设置线索公海池和客户公海池 “转” 字标签
     * @param publicTagPojo
     * @param dataId
     * @param tagData
     */
    public static void setTransferTagData(PublicTag publicTagPojo, Long dataId, JSONObject tagData) {
        if (StringUtil.isNotEmpty(publicTagPojo.getAttr()) && publicTagPojo.getPublicTagMap().containsKey(dataId)) {
            JSONArray tagPojoList = tagData.getJSONArray(publicTagPojo.getAttr());
            tagPojoList = tagPojoList == null ? new JSONArray() : tagPojoList;
            TagPojo tagPojo = new TagPojo(TagEnum.TAG_TRANSFER_PUBLIC_CUSTOMER);
            tagPojo.setMemo(publicTagPojo.getPublicTagMap().get(dataId));
            tagPojoList.add(tagPojo);
            tagData.put(publicTagPojo.getAttr(), tagPojoList);
        }
    }
}
